package com.shuzixiangcun.controller.admin;

import com.github.pagehelper.PageInfo;
import com.shuzixiangcun.entity.Orders;

import com.shuzixiangcun.entity.Users;
import com.shuzixiangcun.service.admin.OrdersServiceImpl;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单表(Orders)表控制层
 *
 * @author makejava
 * @since 2024-06-24 08:26:16
 */
@RestController
@RequestMapping("admin/orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersServiceImpl ordersService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Orders> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.ordersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param orders 实体
     * @return 新增结果
     */


    /**
     * 编辑数据
     *
     * @param orders 实体
     * @return 编辑结果
     */


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */

    @PostMapping("/insertOrder")
    public ResponseEntity insertOrder(Orders orders){
        int num = this.ordersService.insert(orders);
        if (num>0){
            return ResponseEntity.status(200).body("添加成功");
        }
        return ResponseEntity.status(500).body("添加失败");
    }

    @GetMapping("/List")
    public ResponseEntity<PageInfo<Orders>> showList(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam(required = false) String search) {
        return ResponseEntity.ok(this.ordersService.getPageInfo(pageNo,pageSize,search));
    }

    @PostMapping("update")
    public ResponseEntity update(Orders orders){

        String state1=orders.getProductStatus().equals("待发货")?"待收货":orders.getProductStatus().equals("待收货")?"已完成":"啥也不是";

        orders.setProductStatus(state1);
        if (this.ordersService.update(orders)>0){
            return ResponseEntity.status(200)
                    .body("修改成功");
        }
        return ResponseEntity.status(500)
                .body("修改失败");
    }

    @DeleteMapping("delete")
    public ResponseEntity deleteById(Integer id) {
        return ResponseEntity.ok(this.ordersService.deleteById(id));
    }

}

